class Solution(object):
    def maximumUnits(self, boxTypes, truckSize):
        boxTypes.sort(key=lambda x: (-x[1]))
        n = len(boxTypes)
        index = 0
        ans = 0
        while truckSize > 0 and index < n:
            if truckSize >= boxTypes[index][0]:
                truckSize -= boxTypes[index][0]
                ans += boxTypes[index][0] * boxTypes[index][1]
                index += 1
            else:
                ans += boxTypes[index][1] * truckSize
                truckSize = 0
        return ans
